import 'package:flutter/material.dart';

class HeartbeatAnimation extends StatefulWidget {
  final double size;

  const HeartbeatAnimation({super.key, this.size = 25.0});
  @override
  _HeartbeatAnimationState createState() => _HeartbeatAnimationState();
}

class _HeartbeatAnimationState extends State<HeartbeatAnimation>
    with TickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();

    // 创建动画控制器
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );

    // 定义曲线动画
    final Animation<double> curveAnimation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    );

    // 定义动画范围（大小变化）
    _animation = Tween<double>(begin: 1.0, end: 1.2).animate(curveAnimation);

    // 监听动画完成事件，以便进行反向播放
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });

    // 启动动画
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final w = widget.size;
    return AnimatedBuilder(
        animation: _animation,
        builder: (ctext, _) {
          return Transform.scale(
            scale: _animation.value,
            child: Icon(
              Icons.favorite,
              color: Color(0xff652639),
              size: w,
            ),
          );
        });
  }
}
